package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectFileMapper;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.text.Convert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 项目文件Service业务层处理
 *
 * @author zhaonz
 * @date 2023-06-02
 */
@Service
public class ProjectFileServiceImpl extends ServiceImpl<ProjectFileMapper, ProjectFile> implements IProjectFileService {

    private static final Logger logger = LoggerFactory.getLogger(ProjectFileServiceImpl.class);

    @Autowired
    private IProjectScheduleService projectScheduleService;

    @Autowired
    private IProjectManagementService projectManagementService;

    @Autowired
    private IProjectResearchService projectResearchService;

    @Autowired
    private IInviteBidsService iInviteBidsService;

    @Autowired
    private IPocTestingService pocTestingService;

    @Autowired
    private ProjectFileMapper projectFileMapper;

    /**
     * 查询项目文件列表
     *
     * @param projectFile 项目文件
     * @return 项目文件
     */
    @Override
    public List<ProjectFile> list(ProjectFile projectFile) {
        return this.list(new QueryWrapper<>(projectFile));
    }

    /**
     * 查询项目文件
     *
     * @param fileId 项目文件主键
     * @return 项目文件
     */
    @Override
    public ProjectFile selectProjectFileByFileId(Long fileId) {
        return projectFileMapper.selectProjectFileByFileId(fileId);
    }

    /**
     * 查询项目文件列表
     *
     * @param projectFile 项目文件
     * @return 项目文件
     */
    @Override
    public List<ProjectFile> selectProjectFileList(ProjectFile projectFile) {
        return projectFileMapper.selectProjectFileList(projectFile);
    }

    /**
     * @param projectFile
     * @return
     */
    @Override
    public boolean batchSave(List<ProjectFile> projectFile) {
        UpdateWrapper<ProjectSchedule> wrapper = new UpdateWrapper<>();
        wrapper.set("is_with_files", 1).eq("schedule_id", projectFile.get(0).getScheduleId());
        projectScheduleService.update(wrapper);
        return this.saveBatch(projectFile);
    }

    /**
     * 新增项目文件
     *
     * @param projectFile 项目文件
     * @return 结果
     */
    @Override
    public int insertProjectFile(ProjectFile projectFile) {
        projectFile.setCreateTime(DateUtils.getNowDate());
        return projectFileMapper.insertProjectFile(projectFile);
    }

    /**
     * 修改项目文件
     *
     * @param projectFile 项目文件
     * @return 结果
     */
    @Override
    public int updateProjectFile(ProjectFile projectFile) {
        projectFile.setUpdateTime(DateUtils.getNowDate());
        return projectFileMapper.updateProjectFile(projectFile);
    }

    /**
     * 批量删除项目文件
     *
     * @param fileIds 需要删除的项目文件主键
     * @return 结果
     */
    @Override
    public int removeByIds(String fileIds) {
        return this.baseMapper.deleteBatchIds(Lists.newArrayList(Convert.toStrArray(fileIds)));
    }

    /**
     * 批量删除项目文件
     *
     * @param fileIds 需要删除的项目文件主键
     * @return 结果
     */
    @Override
    public int deleteProjectFileByFileIds(String fileIds) {
        return projectFileMapper.deleteProjectFileByFileIds(Convert.toStrArray(fileIds));
    }

    /**
     * 删除项目文件信息
     *
     * @param fileId 项目文件主键
     * @return 结果
     */
    @Override
    public int deleteProjectFileByFileId(Long fileId) {
        return projectFileMapper.deleteProjectFileByFileId(fileId);
    }

    /**
     * @param param
     * @param list
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean uploadFiles(Map<String, Object> param, List<ProjectFile> list) throws Exception {

        if (!CollectionUtils.isEmpty(list)) {
            this.saveBatch(list);
        }

        ProjectSchedule schedule = projectScheduleService.getById(MapUtils.getLong(param,"scheduleId"));

        if ("DEMAND_RESEARCH".equals(schedule.getProjectSchedule())) {
            if ("1".equals(MapUtils.getString(param,"projectResearch"))) {
                ProjectResearch research = MapMapper.INSTANCE.toResearch(param);
                projectResearchService.save(research);
            }

            ProjectManagement management = MapMapper.INSTANCE.toManagement(param);
            projectManagementService.updateById(management);
        } else if ("POC_TEST".equals(schedule.getProjectSchedule())) {
            PocTesting pocTesting = MapMapper.INSTANCE.toPocTesting(param);
            pocTestingService.save(pocTesting);
        } else if ("INVITE_BIDS".equals(schedule.getProjectSchedule())){
            InviteBids inviteBids = MapMapper.INSTANCE.toInviteBids(param);
            iInviteBidsService.save(inviteBids);
        } else if ("CONCLUDE_A_CONTRACT".equals(schedule.getProjectSchedule())){
            ProjectManagement management = MapMapper.INSTANCE.toManagement(param);
            projectManagementService.updateById(management);
        }

        boolean approvalAudit = false;
        if (StringUtils.isNotNull(param.get("approvalAudit"))){
            approvalAudit = MapUtils.getBoolean(param, "approvalAudit");
        } else {
            approvalAudit = true;
        }

        UpdateWrapper<ProjectSchedule> wrapper = new UpdateWrapper<ProjectSchedule>()
                .set("finding_of_audit", approvalAudit ? 1 : 0)
                .set("is_with_files", CollectionUtils.isEmpty(list) ? 0 : 1)
                .eq("schedule_id", schedule.getScheduleId());

        if (StringUtils.isNotNull(param.get("approvalTime"))){
            wrapper.set("audit_time", DateUtils.dateTime(DateUtils.YYYY_MM_DD, MapUtils.getString(param ,"approvalTime")));
        }

        return projectScheduleService.update(wrapper);
    }
}
